import {
  baseUrl
} from './http.js'

module.exports = {
  request: function (url, methodType, data) {
    let fullUrl = `${baseUrl}${url}`
    let LoginInfo = wx.getStorageSync('LoginInfo') ? wx.getStorageSync('LoginInfo') : ''
    let token = LoginInfo.token
    // console.log(token);
    //(wx.showLoading)显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    // wx.showLoading({
    //   title: "数据请求中"
    // });
    return new Promise((resolve, reject) => {
      wx.request({
        url: fullUrl,
        method: methodType,
        data: data,
        header: {
          'content-type': 'application/json', // 默认值
          'authentication': token,
        },
        success(res) {
          // console.log(res);
          if (res.data.code == 1) {
            resolve(res.data)
            // wx.hideLoading()
          } else {
            //手动关闭loading提示框
            wx.hideLoading()
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
            wx.setStorageSync('isLogin', true)
            reject(res.data.message)
          }
        },
        fail(res) {
          console.log(res);
          wx.showToast({
            title: '接口请求错误',
            icon: 'none'
          })
          reject('接口请求错误')
        }
      })
    })
  }
}